﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Server_Code.Model.Processing;

namespace Server_Code.Model.Processing
{
    public static class CEmergency
    {
        private static DatabaseEntities _db;

        //Get list Emergency from database
        public static List<Emergency> GetList()
        {
            _db = new DatabaseEntities();
            return _db.Emergencies.ToList();
        }

        //Get max id in ID collum
        public static int GetMaxId()
        {
            _db = new DatabaseEntities();
            int maxId = 1;
            try
            {
                maxId = _db.Emergencies.Max(x => x.ID) + 1;
            }
            catch (Exception)
            {
                
            }
            return maxId;
        }

        //Get emergency by Id
        public static Emergency Get(int i)
        {
            _db = new DatabaseEntities();
            Emergency emergency = new Emergency();
            try
            {
                emergency =  _db.Emergencies.First(x => x.ID == i);
            }
            catch (Exception)
            {
                
            }
            return emergency;
        }

        public static bool Add(Emergency emergency)
        {
            _db = new DatabaseEntities();
            bool check = true;
            try
            {
                _db.Emergencies.AddObject(emergency);
                _db.SaveChanges();
            }
            catch (Exception)
            {
                check = false;
            }
            return check;
        }

        public static Emergency New(int id,string phoneNumber, string longitude, string latitude, string message, DateTime time,int status)
        {
            var emergency = new Emergency
                                      {
                                          ID = id,
                                          PhoneNumber = phoneNumber,
                                          Longitude = longitude,
                                          Latitude = latitude,
                                          Message = message,
                                          Time = time.ToString(),
                                          Status = status
                                      };
            return emergency;
        }
    }
}